//
//  TCM_AuctionDetailFooterView.swift
//  TCM_TradingProject
//  拍卖详情底部视图
//
//  Created by xiaofeiZhang on 2018/8/29.
//  Copyright © 2018年 lvchunhai. All rights reserved.
//

import UIKit
import SnapKit
import RxCocoa
import RxSwift

class TCM_AuctionDetailFooterView: UIView {
    var tableView: UITableView?
    let disposeBag = DisposeBag()
    var selectedBtn: UIButton?

    let titles = ["药材详情", "质检报告", "价格走势"]
    var titleView: UIView?
    private lazy var detailTableView: UITableView = {
        
        let detailTableView = UITableView.init(frame: CGRect.zero, style: UITableViewStyle.plain)
        detailTableView.delegate = self
        detailTableView.dataSource = self
        detailTableView.separatorStyle = .none
        detailTableView.isScrollEnabled = false
        detailTableView.register(UINib.init(nibName: "TCM_AuctionDetailBotomCell", bundle: nil), forCellReuseIdentifier: "DetailCell")
        
        return detailTableView
    }()
    
    private lazy var detailReportView: TCM_AuctionDetailReportView = {
        
        let nib = UINib.init(nibName: "TCM_AuctionDetailReportView", bundle: nil)
        let detailReportView = nib.instantiate(withOwner: self, options: nil).first as! TCM_AuctionDetailReportView
        
        return detailReportView
    }()
    
    private lazy var detailChartView: TCM_AuctionDetailChartView = {
        
        let nib = UINib.init(nibName: "TCM_AuctionDetailChartView", bundle: nil)
        let detailChartView = nib.instantiate(withOwner: self, options: nil).first as! TCM_AuctionDetailChartView
        
        return detailChartView
    }()
    
    private lazy var contentView: UIScrollView = {
        
        let contentView = UIScrollView.init()
       
        contentView.backgroundColor = UIColor.white
        let width =  CGFloat(titles.count) * TCM_ScreenWidth
        contentView.contentSize = CGSize(width: width, height: 0)
        contentView.isPagingEnabled = true
        contentView.showsVerticalScrollIndicator = false
        contentView.showsHorizontalScrollIndicator = false
        contentView.isScrollEnabled = false
        
        contentView.addSubview(detailTableView)
        contentView.addSubview(detailReportView)
        contentView.addSubview(detailChartView)
        
        return contentView
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.initialSunViews()
        self.setupConstraints()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func initialSunViews() {
        self.initialTitleViews()
        self.addSubview(contentView)
    }
    
    fileprivate func initialTitleViews() {
        titleView = UIView()
        titleView?.backgroundColor = UIColor.white
        self.addSubview(titleView!)

        for (index, value) in titles.enumerated() {

            let titleBtn = self.titleBtnFactory(title: value, tag: index + 100)
            titleView?.addSubview(titleBtn)

            titleBtn.snp.makeConstraints ({ [weak self] (make) in
                make.width.equalTo(TCM_ScreenWidth * (1.0 / 3.0))
                make.top.bottom.equalTo(0)
                make.leading.equalTo(TCM_ScreenWidth * (1.0 / 3.0) * CGFloat(index))
            })
        }
    }
    
    fileprivate func setupConstraints() {
        titleView?.snp.makeConstraints({ (make) in
            make.top.leading.trailing.equalTo(0)
            make.height.equalTo(44.0)
        })
        contentView.snp.makeConstraints { (make) in
            make.top.equalTo(titleView!.snp.bottom).offset(1)
            make.leading.trailing.bottom.equalTo(0)
        }
        detailTableView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(10)
            make.leading.equalToSuperview()
            make.height.equalToSuperview()
            make.width.equalTo(TCM_ScreenWidth)
        }
        detailReportView.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.leading.equalToSuperview().offset(TCM_ScreenWidth)
            make.height.equalTo(420.0)
            make.width.equalTo(TCM_ScreenWidth)
        }
        detailChartView.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.leading.equalToSuperview().offset(TCM_ScreenWidth * 2.0)
            make.height.equalTo(241.0)
            make.width.equalTo(TCM_ScreenWidth)
        }
        
    }
}

extension TCM_AuctionDetailFooterView {
    fileprivate func titleBtnFactory(title: String, tag: Int) -> UIView {
        
        let contentV = UIView()
        contentV.backgroundColor = UIColor.white
        let v = UIView()

        let btn = UIButton(type: .custom)
        btn.setTitle(title, for: .normal)
        btn.backgroundColor = UIColor.white
        btn.titleLabel?.font = TCM_SystemFont(14.0)
        btn.setTitleColor(TCM_HexRGB(0x3B3B3B), for: .normal)
        btn.setTitleColor(TCM_HexRGB(0x693C26), for: .selected)
        btn.tag = tag
        btn.addTarget(self, action: #selector(titleBtnClick(_:)), for: .touchUpInside)
        
        
        v.backgroundColor = TCM_HexRGB(0x552D1D)
        v.isHidden = tag - 100 != 0
        v.tag = 998
        
        contentV.addSubview(btn)
        contentV.addSubview(v)
        contentV.tag = tag
        
        btn.snp.makeConstraints { (make) in
            make.width.equalToSuperview()
            make.height.equalToSuperview()
            make.center.equalToSuperview()
        }
        
        v.snp.makeConstraints { (make) in
            make.width.equalTo(20)
            make.height.equalTo(4)
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        if tag - 100 == 0 {
            self.selectedBtn = btn
            btn.isSelected = true
            btn.titleLabel?.font = TCM_BoldFont(14.0)
        }
        return contentV
    }
    
    @objc fileprivate func titleBtnClick(_ button: UIButton) {
        if let _ = self.selectedBtn {
            let lastTitle = self.titleView?.viewWithTag(self.selectedBtn!.tag)
            self.selectedBtn!.isSelected = false
            self.selectedBtn!.titleLabel?.font = TCM_SystemFont(14.0)
            let lastLineView = lastTitle?.viewWithTag(998)
            lastLineView?.isHidden = true
        }
        
        let index = button.tag - 100

        self.contentView.setContentOffset(CGPoint(x: CGFloat(index) * TCM_ScreenWidth, y: 0), animated: true)
        button.isSelected = true
        button.titleLabel?.font = TCM_BoldFont(14.0)
        
        let lineView = self.titleView?.viewWithTag(button.tag)?.viewWithTag(998)
        lineView?.isHidden = false

        self.selectedBtn = button
        if index == 0 {
            self.frame = CGRect(x: self.x, y: self.y, width: self.width, height: 246)
        } else if index == 1 {
            self.frame = CGRect(x: self.x, y: self.y, width: self.width, height: 464)
        } else {
            self.frame = CGRect(x: self.x, y: self.y, width: self.width, height: 285)
        }
        tableView?.reloadData()
    }
}

extension TCM_AuctionDetailFooterView: UITableViewDelegate, UITableViewDataSource {
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 6
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "DetailCell") as! TCM_AuctionDetailBotomCell
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 33.0
    }
    
    
}
